﻿using System;
using System.Collections.Generic;
using System.Text;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using Runda.EShop.Dal;
using System.Data;
using System.Data.Common;

namespace Runda.EShop.Bll
{
    public class OrderBll:BllBase
    {
        #region 订单流程
     
        /// <summary>
        /// 客户初次下订单
        /// </summary>
        /// <param name="entity"></param>
        public void CreateOrder(BizOrderEntity entity,UserTokenData LoginUser)
        {
            //从购物车购造订单明细
            BizShopCartDal dal = new BizShopCartDal();
            DataTable dt = dal.GetShopcartByCustomerID(entity.CustomerID);
            List<BizOrderDetailEntity> list = new List<BizOrderDetailEntity>();
            List<BizOrderDetailEntity> list_2 = new List<BizOrderDetailEntity>();
            bool hasEmpty = false;
            foreach (DataRow row in dt.Rows)
            {
                bool flag = false;
                BizOrderDetailEntity ent = new BizOrderDetailEntity();
                ent.OrderID = entity.OrderID;
                ent.ProductID = new Guid(row["ProductID"].ToString());
                ent.ProductName = row["ProductName"].ToString();
                ent.ProductCode = row["ProductCode"].ToString();
                ent.ProductStd = row["Standard"].ToString();
                ent.Unit = row["Unit"].ToString();
                ent.Quantity = Convert.ToDecimal(row["Quantity"].ToString()); 
                ent.HopeDate = DateTime.Now;
                ent.Remark = row["Remark"] == DBNull.Value ? string.Empty:row["Remark"].ToString();
                ent.BatchCode = row["BatchCode"].ToString();
                ent.DueDate = Convert.ToDateTime(row["DueDate"].ToString());
                ent.Inventory = Convert.ToDecimal(row["Inventory"].ToString());
                ent.Deleted = false;
                ent.FollowResult = 0;

                if (LoginUser.EntType == (int)EnumHelper.EnterpriseType.Terminal)
                {
                    if (row["Inventory"] == DBNull.Value || Convert.ToDecimal(row["Inventory"].ToString()) == 0)
                    {
                        flag = true;
                        hasEmpty = true;
                    }
                }
                if (flag)
                {
                    list_2.Add(ent);
                }
                else
                {
                    list.Add(ent);
                }
            }

            //entity.OrderDetails = list;
            //生成订单号
            entity.OrderCode = GenerateOrderCode(DateTime.Now).ToString();

            //生成状态
            BizOrderStatusEntity orderStatusEntity = new BizOrderStatusEntity();
            orderStatusEntity.OrderID = entity.OrderID;
            orderStatusEntity.Status = (int)EnumHelper.OrderStatus.Submit;
            orderStatusEntity.StatusName = EnumDescAttribute.GetFieldText(EnumHelper.OrderStatus.Submit);
            orderStatusEntity.CreatedAt = DateTime.Now;
            orderStatusEntity.CreatedBy = entity.CustomerID;
            //orderStatusEntity.CreatedByName = 
            orderStatusEntity.Deleted = false;

            //Service提交状态表
            SysOrderServiceStatusEntity orderServiceStatusEntity = new SysOrderServiceStatusEntity();
            orderServiceStatusEntity.OrderID = entity.OrderID;
            orderServiceStatusEntity.NextTime = DateTime.Now;
            orderServiceStatusEntity.Status = (int)EnumHelper.OrderServiceStatus.Pending;
            orderServiceStatusEntity.Deleted = false;
            //提交订单
            base.SafeDbExecute(delegate(IDbTransaction iTran)
            {
                DbTransaction tran = iTran as DbTransaction; 
                BizShopCartDal shopCartDal = new BizShopCartDal(tran);
                shopCartDal.Delete<BizShopCartEntity>(" and CustomerID='" + entity.CustomerID + "'");

                BizOrderDetailDal orderDetailDal = new BizOrderDetailDal(tran);
                BizOrderStatusDal orderStatusDal = new BizOrderStatusDal(tran);
                SysOrderServiceStatusDal sysOrderDal = new SysOrderServiceStatusDal(tran);
                if (list.Count > 0)
                {
                    shopCartDal.Insert(entity);

                    orderDetailDal.BulkInsert(list);

                    orderStatusDal.Insert(orderStatusEntity);

                    sysOrderDal.Insert(orderServiceStatusEntity);
                }

                if (hasEmpty)
                {
                    entity.OrderID = Guid.NewGuid();
                    entity.OrderCode = entity.OrderCode + "_1";
                    entity.OrderType = (int)EnumHelper.OrderType.EmptyOrder;
                    entity.OrderEmptyStatus = (int)EnumHelper.OrderEmptyStatus.Pending;
                    shopCartDal.Insert(entity);

                    foreach (BizOrderDetailEntity detail2 in list_2)
                    {
                        detail2.OrderID = entity.OrderID;
                        detail2.FollowResult = 0;//后续库存生成新订单用
                    }
                    orderDetailDal.BulkInsert(list_2);

                    orderStatusEntity.ID = Guid.NewGuid();
                    orderStatusEntity.OrderID = entity.OrderID;
                    orderStatusDal.Insert(orderStatusEntity);

                    //空订单不需要同步到WMS中
                    //orderServiceStatusEntity.ID = Guid.NewGuid();
                    //orderServiceStatusEntity.OrderID = entity.OrderID;
                    //sysOrderDal.Insert(orderServiceStatusEntity);
                }
            });

            try
            {
                //把订购产品放入常用产品 有待优化
                foreach (BizOrderDetailEntity detail in list)
                {
                    BizCustomerFavoriteEntity Fav = new BizCustomerFavoriteEntity();
                    Fav.ProductID = detail.ProductID;
                    Fav.CustomerID = entity.CustomerID;
                    Fav.CreatedAt = DateTime.Now;
                    Fav.Deleted = false;
                    CustomerBll cusBll = new CustomerBll();
                    cusBll.AddCustomerFavorite(Fav);
                }

                //发消息通知给订单角色用户
                SysMessageBll msgBll = new SysMessageBll();
                StringBuilder sbMsgContent = new StringBuilder();
                sbMsgContent.Append("客户").Append(LoginUser.UserName).Append("[").Append(LoginUser.EntName)
                    .Append("]在").Append(entity.CreatedOn).Append("下订单:")
                    .Append("<a href='../OrderManage/OrderView.aspx?OrderID=").Append(entity.OrderID).Append("' target='_blank'>").Append(entity.OrderCode).Append("</a>，<a href='../OrderManage/OrderList.aspx' target='_blank'>点此到订单页面</a>。");
                msgBll.SendMessageToUser(new Guid(Runda.EShop.Common.Utility.ConstHelper.USER_ROLE_ORDER), "新订单提醒",sbMsgContent.ToString());
            }
            catch
            { }
             
        }

        /// <summary>
        /// Wms拆分订单
        /// </summary>
        public void PatchOrder(OrderDetailPatchWrapperEntity[] list)
        {
            if (list.Length == 0) return;
            Guid OrderID = new Guid(list[0].OrderID);

            BizOrderEntity orderEntity = this.Get<BizOrderEntity>(OrderID);

            List<BizOrderDetailPatchEntity> detailPatchList = new List<BizOrderDetailPatchEntity>();
            BizOrderDetailPatchEntity detailPatchEntity;
            foreach (OrderDetailPatchWrapperEntity item in list)
            {
                detailPatchEntity = new BizOrderDetailPatchEntity();
                detailPatchEntity.ID = Guid.NewGuid();
                detailPatchEntity.OrderID = orderEntity.OrderID;
                detailPatchEntity.DetailID = new Guid(item.DetailID);
                detailPatchEntity.ProductID = new Guid(item.ProductID);
                detailPatchEntity.ProductCode = item.ProductCode;
                detailPatchEntity.BatchCode = item.BatchCode;
                detailPatchEntity.DueDate = Convert.ToDateTime(item.DueDate);
                detailPatchEntity.ActualQty = Convert.ToDecimal(item.AssignQty);
                detailPatchEntity.AssignQty = Convert.ToDecimal(item.AssignQty);
                detailPatchEntity.ConfirmStatus = "未确认";
                detailPatchEntity.Deleted = false;
                detailPatchList.Add(detailPatchEntity);
            }


            BizOrderStatusEntity processEntity = new BizOrderStatusEntity();
            processEntity.OrderID = orderEntity.OrderID;
            processEntity.Status = (int)EnumHelper.OrderStatus.CompanyConfirm;
            processEntity.StatusName = EnumDescAttribute.GetFieldText(EnumHelper.OrderStatus.CompanyConfirm);
            processEntity.CreatedAt = DateTime.Now;
            processEntity.Deleted = false;

            base.SafeDbExecute(delegate(IDbTransaction iTran)
            {
                DbTransaction tran = iTran as DbTransaction;
                BizOrderDetailPatchDal dal = new BizOrderDetailPatchDal(tran);
                BizOrderDal orderDal = new BizOrderDal(tran);
                BizOrderStatusDal processDal = new BizOrderStatusDal(tran);
                //批量插入到Patch
                dal.BulkInsert(detailPatchList);

                //更新订单状态
                orderDal.UpdateOrderStatus(orderEntity.OrderID, EnumHelper.OrderStatus.CompanyConfirm);

                //写入Biz_Order_Status
                processDal.Delete<BizOrderStatusEntity>(" and OrderID='" + OrderID + "' and [Status]=" + processEntity.Status);
                processDal.Insert(processEntity);
             });

            try
            {
                //发消息提醒给客户
                SysMessageEntity msgEntity = new SysMessageEntity();
                msgEntity.Title = "订单确认提醒";
                msgEntity.Content = "你有订单" + orderEntity.OrderCode + "分配库存，请确认!";
                msgEntity.ObjectType = (int)EnumHelper.UserType.Customer;
                msgEntity.ObjectID = orderEntity.CustomerID;
                msgEntity.IsRead = 0;
                msgEntity.CreatedAt = DateTime.Now;
                msgEntity.Deleted = false;
                SysMessageDal msgDal = new SysMessageDal();
                msgDal.Insert(msgEntity);
            }
            catch
            { }

        }

        public void ConfirmOrder(Guid OrderID, UserTokenData LoginUser)
        {
                //生成状态
            BizOrderStatusEntity orderStatusEntity = new BizOrderStatusEntity();
            orderStatusEntity.OrderID = OrderID;
            orderStatusEntity.Status = (int)EnumHelper.OrderStatus.CustomerConfirm;
            orderStatusEntity.StatusName = EnumDescAttribute.GetFieldText(EnumHelper.OrderStatus.CustomerConfirm);
            orderStatusEntity.CreatedAt = DateTime.Now;
            orderStatusEntity.CreatedBy =Guid.Empty;
            //orderStatusEntity.CreatedByName = 
            orderStatusEntity.Deleted = false;

             //Service提交状态表
            SysOrderServiceStatusEntity orderServiceStatusEntity = new SysOrderServiceStatusEntity();
            orderServiceStatusEntity.OrderID = OrderID;
            orderServiceStatusEntity.NextTime = DateTime.Now;
            orderServiceStatusEntity.Status = (int)EnumHelper.OrderServiceStatus.Pending;
            orderServiceStatusEntity.Deleted = false;

            base.SafeDbExecute(delegate(IDbTransaction iTran)
            {
                DbTransaction tran = iTran as DbTransaction;

                BizOrderDal dal = new BizOrderDal(tran);

                BizOrderProcessDal procDal = new BizOrderProcessDal(tran);

                dal.Delete<BizOrderStatusEntity>(" and OrderID='" + OrderID + "' and [Status]=" + ((int)EnumHelper.OrderStatus.CustomerConfirm).ToString());
                dal.Insert(orderStatusEntity);

                dal.UpdateOrderStatus(OrderID, EnumHelper.OrderStatus.CustomerConfirm);

                procDal.Insert(orderServiceStatusEntity);

            });

            try
            {
                //发消息通知给订单角色用户
                BizOrderEntity orderEntity = this.Get<BizOrderEntity>(OrderID);
                SysMessageBll msgBll = new SysMessageBll();
                StringBuilder sbMsgContent = new StringBuilder();
                sbMsgContent.Append("客户").Append(LoginUser.UserName).Append("[").Append(LoginUser.EntName)
                .Append("]在").Append(DateTime.Now).Append("已确认订单:")
                .Append("<a href='../OrderManage/OrderView.aspx?OrderID=").Append(orderEntity.OrderID).Append("' target='_blank'>").Append(orderEntity.OrderCode).Append("</a>，<a href='../OrderManage/OrderList.aspx' target='_blank'>点此到订单页面</a>。");
                msgBll.SendMessageToUser(new Guid(Runda.EShop.Common.Utility.ConstHelper.USER_ROLE_ORDER), "客户已确认订单提醒", sbMsgContent.ToString());
            }
            catch
            { }
        }

        /// <summary>
        /// 修改订单
        /// </summary>
        /// <remarks>
        /// 原有订单改为已提交状态，删除原有Detail与DetailPatch数据，插入新修改的detail
        /// </remarks>
        /// <param name="OrderID"></param>
        /// <param name="details"></param>
        public void EditOrder(Guid OrderID, List<BizOrderDetailEntity> details)
        {   
            
            //Service提交状态表
            SysOrderServiceStatusEntity orderServiceStatusEntity = new SysOrderServiceStatusEntity();
            orderServiceStatusEntity.OrderID = OrderID;
            orderServiceStatusEntity.NextTime = DateTime.Now;
            orderServiceStatusEntity.Status = (int)EnumHelper.OrderServiceStatus.Pending;
            orderServiceStatusEntity.Deleted = false;

            base.SafeDbExecute(delegate(IDbTransaction iTran)
            {
                DbTransaction tran = iTran as DbTransaction;
                BizOrderDal dal = new BizOrderDal(tran);
                dal.UpdateOrderStatus(OrderID, EnumHelper.OrderStatus.Submit);

                BizOrderDetailDal orderDetailDal = new BizOrderDetailDal(tran);

                orderDetailDal.Delete<BizOrderDetailEntity>(" and OrderID='" + OrderID + "'");

                orderDetailDal.Delete<BizOrderDetailPatchEntity>(" and OrderID='" + OrderID + "'");

                orderDetailDal.BulkInsert(details);

                SysOrderServiceStatusDal sysOrderDal = new SysOrderServiceStatusDal(tran);
                sysOrderDal.Insert(orderServiceStatusEntity);
            });
        }

        /// <summary>
        /// 订单取消
        /// </summary>
        /// <remarks>
        /// 更新订单状态为"关闭状态"，
        /// </remarks>
        /// <param name="OrderID"></param>
        public void CancelOrder(Guid OrderID,UserTokenData Manager, string CancelReason)
        {
            BizOrderEntity entity = this.Get<BizOrderEntity>(OrderID);

            SysMessageEntity msgEntity = new SysMessageEntity();
            msgEntity.Title = "订单" + entity.OrderCode+ "取消提醒";
            msgEntity.Content = "订单" + entity.OrderCode + "由于["+CancelReason + "]被"+Manager.UserName+"["+Manager.UserCode+"]"+"取消";
            msgEntity.ObjectType = (int)EnumHelper.UserType.Customer;
            msgEntity.ObjectID = entity.CustomerID;
            msgEntity.IsRead = 0;
            msgEntity.CreatedAt = DateTime.Now;
            msgEntity.Deleted = false;

            //生成状态
            BizOrderStatusEntity orderStatusEntity = new BizOrderStatusEntity();
            orderStatusEntity.OrderID = entity.OrderID;
            orderStatusEntity.Status = (int)EnumHelper.OrderStatus.Cancel;
            orderStatusEntity.StatusName = EnumDescAttribute.GetFieldText(EnumHelper.OrderStatus.Cancel);
            orderStatusEntity.StatusDesc = CancelReason;
            orderStatusEntity.CreatedAt = DateTime.Now;
            orderStatusEntity.CreatedBy = entity.CustomerID;
            //orderStatusEntity.CreatedByName = 
            orderStatusEntity.Deleted = false;

            //Service提交状态表
            SysOrderServiceStatusEntity orderServiceStatusEntity = new SysOrderServiceStatusEntity();
            orderServiceStatusEntity.OrderID = OrderID;
            orderServiceStatusEntity.NextTime = DateTime.Now;
            orderServiceStatusEntity.Status = (int)EnumHelper.OrderServiceStatus.Pending;
            orderServiceStatusEntity.Deleted = false;

            base.SafeDbExecute(delegate(IDbTransaction iTran)
            {
                DbTransaction tran = iTran as DbTransaction;
                BizOrderDal dal = new BizOrderDal(tran);
                dal.UpdateOrderStatus(OrderID, EnumHelper.OrderStatus.Cancel);

                BizOrderStatusDal orderStatusDal = new BizOrderStatusDal(tran);
                orderStatusDal.Insert(orderStatusEntity);

                SysMessageDal msgDal = new SysMessageDal(tran);
                msgDal.Insert(msgEntity);

                SysOrderServiceStatusDal ossDal = new SysOrderServiceStatusDal(tran);
                ossDal.Insert(orderServiceStatusEntity);

            });
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询订单
        /// </summary>
        /// <param name="parms"></param>
        /// <param name="OrderBy"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="RecordCount"></param>
        /// <returns></returns>
        public DataTable GetOrderList(List<SqlDbParameter> parms, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            BizOrderDal dal = new BizOrderDal();
            return dal.GetOrderList(parms, OrderBy, PageSize, PageIndex, out RecordCount);
        }

        public DataTable QueryOrderList(List<SqlDbParameter> parms, string ProductName, string CateCode, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            BizOrderDal dal = new BizOrderDal();
            return dal.QueryOrderList(parms, ProductName,CateCode, OrderBy, PageSize, PageIndex, out RecordCount);
        }

        public DataTable ExportOrderList(List<SqlDbParameter> parms)
        {
            BizOrderDal dal = new BizOrderDal();
            return dal.ExportOrderList(parms);
        }
        #endregion

        #region 空订单处理
        /// <summary>
        /// 在空订单基础上生成新订单
        /// </summary>
        /// <remarks>
        /// 更新原订单明细状态，生成新订单
        /// </remarks>
        /// <param name="orderID"></param>
        /// <param name="details"></param>
        public void GenerateOrderByEmptyOrder(Guid orderID, List<Guid> details,Guid manager)
        {
            //构造订单主体信息
            BizOrderEntity orderEntity = this.Get<BizOrderEntity>(orderID);
            orderEntity.OrderID = Guid.NewGuid();
            orderEntity.OrderCode = GenerateOrderCode(DateTime.Now).ToString();
            //orderEntity.CustomerID = base.User.Identity.LoginUser.UserID;
            orderEntity.OrderDate = DateTime.Now;
            orderEntity.OrderStatus = (int)EnumHelper.OrderStatus.Submit;
            orderEntity.OrderType = (int)EnumHelper.OrderType.NormalOrder;
            orderEntity.CreatedOn = DateTime.Now;
            orderEntity.CreatedManager = manager;
            //orderEntity.ReceiveName = addrEnity.Name;
            //orderEntity.ReceiverPhone = addrEnity.Phone;
            //orderEntity.ReceiveAddr = addrEnity.Address;
            //orderEntity.Memo = txtMemo.Text;
            //orderEntity.Grade = 0;
            orderEntity.Deleted = false;

            //构造客户信息
            BizCustomerEntity customerEntity = this.Get<BizCustomerEntity>(orderEntity.CustomerID);
            BizEnterpriseEntity entEntity = this.Get<BizEnterpriseEntity>(customerEntity.EntID);
            UserTokenData loginUser = new UserTokenData(customerEntity.CustomerID, customerEntity.CustomerName, customerEntity.CustomerCode, entEntity.EntID, entEntity.EntCode, entEntity.EntName, entEntity.cCO, entEntity.EntType); 
             
            //构造订单明细信息
            List<BizOrderDetailEntity> list = new List<BizOrderDetailEntity>();
            BizOrderDetailEntity entity = null;
            foreach (Guid itemID in details)
            {
                BizOrderDetailEntity originalDetail = this.Get<BizOrderDetailEntity>(itemID);
                entity = new BizOrderDetailEntity();
                entity.OrderID = orderEntity.OrderID;
                entity.ProductID = originalDetail.ProductID;
                entity.ProductCode = originalDetail.ProductCode;
                entity.ProductName = originalDetail.ProductName;
                entity.ProductStd = originalDetail.ProductStd;
                entity.Quantity = originalDetail.Quantity;
                entity.Unit = originalDetail.Unit;
                entity.DueDate = originalDetail.DueDate;
                entity.Inventory = originalDetail.Inventory;
                entity.Quantity = originalDetail.Quantity;
                entity.HopeDate = originalDetail.HopeDate;
                entity.Remark = originalDetail.Remark;
                entity.FollowResult = 0;
                entEntity.Deleted = false;
                list.Add(entity);
            }
             
            //生成状态
            BizOrderStatusEntity orderStatusEntity = new BizOrderStatusEntity();
            orderStatusEntity.OrderID = entity.OrderID;
            orderStatusEntity.Status = (int)EnumHelper.OrderStatus.Submit;
            orderStatusEntity.StatusName = EnumDescAttribute.GetFieldText(EnumHelper.OrderStatus.Submit);
            orderStatusEntity.CreatedAt = DateTime.Now;
            orderStatusEntity.CreatedBy = orderEntity.CustomerID;
            //orderStatusEntity.CreatedByName = 
            orderStatusEntity.Deleted = false;

            //Service提交状态表
            SysOrderServiceStatusEntity orderServiceStatusEntity = new SysOrderServiceStatusEntity();
            orderServiceStatusEntity.OrderID = entity.OrderID;
            orderServiceStatusEntity.NextTime = DateTime.Now;
            orderServiceStatusEntity.Status = (int)EnumHelper.OrderServiceStatus.Pending;
            orderServiceStatusEntity.Deleted = false;

            //提交订单
            base.SafeDbExecute(delegate(IDbTransaction iTran)
            {
                DbTransaction tran = iTran as DbTransaction;
                BizOrderDal shopCartDal = new BizOrderDal(tran);

                shopCartDal.Insert(orderEntity); 

                //检查订单是否存在未处理订单，如果有，则更新为1（补货中），否则更新为2（补货完成）
                shopCartDal.UpdateOrderEmptyStatus(orderID, details);

                BizOrderDetailDal orderDetailDal = new BizOrderDetailDal(tran);
                orderDetailDal.BulkInsert(list);

                foreach (Guid original in details)
                {
                    orderDetailDal.UpdateOrderDetailFollowResult(original, 1);
                }

                BizOrderStatusDal orderStatusDal = new BizOrderStatusDal(tran);
                orderStatusDal.Insert(orderStatusEntity);

                SysOrderServiceStatusDal sysOrderDal = new SysOrderServiceStatusDal(tran);
                sysOrderDal.Insert(orderServiceStatusEntity);

               
            });

            try
            { 
                //发消息通知给订单角色用户
                SysMessageBll msgBll = new SysMessageBll();
                StringBuilder sbMsgContent = new StringBuilder();
                sbMsgContent.Append("客户").Append(loginUser.UserName).Append("[").Append(loginUser.EntName)
                    .Append("]在").Append(orderEntity.CreatedOn).Append("下订单:")
                    .Append("<a href='../OrderManage/OrderView.aspx?OrderID=").Append(entity.OrderID).Append("' target='_blank'>").Append(orderEntity.OrderCode).Append("</a>，<a href='../OrderManage/OrderList.aspx' target='_blank'>点此到订单页面</a>。");
                msgBll.SendMessageToUser(new Guid(Runda.EShop.Common.Utility.ConstHelper.USER_ROLE_ORDER), "新订单提醒", sbMsgContent.ToString());
            }
            catch
            { }
        }
        #endregion

        #region Help method
        /// <summary>
        /// 生成订单号
        /// </summary>
        /// <param name="OrderDate"></param>
        /// <returns></returns>
        public string GenerateOrderCode(DateTime OrderDate)
        {
            int day = OrderDate.Year * 10000 + OrderDate.Month * 100 + OrderDate.Day;
            SysSerialNumberDal dal = new SysSerialNumberDal();
            SysSerialNumberEntity entity = dal.Get<SysSerialNumberEntity>(" and [FormatedDay] = " + day);
            if (entity != null)
            {
                entity.MaxNumber += 1;
                dal.Update(entity);
            }
            else
            {
                entity = new SysSerialNumberEntity();
                entity.ID = Guid.NewGuid();
                entity.MaxNumber = 1;
                entity.FormatedDay = day;
                dal.Insert(entity);
            }
            long num = entity.FormatedDay * 10000 + entity.MaxNumber;

            return "E" + num.ToString();
        }
        #endregion
    }
}
